package com.infocoll.pachou.biz;

import com.infocoll.pachou.bean.User;
import com.infocoll.pachou.repository.UserRepository;
import com.infocoll.pachou.util.PcConstants;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    PcConstants pcConstants;

    public String save(){
        // 内存数据库操作
        User user1 = new User();
        user1.setUserxuhao("1");
        user1.setUserage("1111");
        userRepository.save(user1);

        User user2 = new User();
        user2.setUserxuhao("2");
        user2.setUserage("2222");
        userRepository.save(user2);

        User user3 = new User();
        user3.setUserxuhao("3");
        user3.setUserage("33333");
        userRepository.save(user3);


        return "save ok";
    }
    public String saveAll(List<User> users){
        // 内存数据库操作
        userRepository.saveAll(users);
        return "save all ok";
    }
    public String saveOne(User user){
        // 内存数据库操作
        userRepository.save(user);
        return "save all ok";
    }
    public Optional<User> findByUser_langAndUser_idno(String user_lang, String user_idno){
        // 内存数据库操作
        return userRepository.findByUserlangAndUserxuhao(user_lang, user_idno);
    }
    public Iterable<User> findByUserName(String user_lang, String user_name){
        // 内存数据库操作
        return userRepository.findAllByUserlangAndUsercmnnameOrUserlatname(user_lang, user_name, user_name);
    }
    public Iterable<User> findAll(){
        // 内存数据库操作
        return userRepository.findAll();
    }

    public long getcount(){
        // 内存数据库操作
        return userRepository.count();
    }



    public void parse(String directory, String user_lang) throws Exception{
        if(directory==null){
            directory = "C:\\Users\\Erison\\Desktop\\nbhlj";
            System.out.println(" 使用默认目录 ");
        }
        File dire = new File(directory);
        if(dire==null){
            System.out.println("目录不存在，请输入目录！ ");
            return;
        }
        File[] files = dire.listFiles();


        Arrays.stream(files).forEach(file-> {
            System.out.println("file = " + file);

            try {

                User user = new User();

                user.setUserlang(user_lang);

                // Document doc = Jsoup.parse(file, "GBK", "");
                Document doc = Jsoup.parse(file, "UTF-8", "");

                user.setUserxuhao(file.getName().substring(0,file.getName().indexOf(".")));
                user.setUserphoto(getElement(1, doc, "body > div:nth-child(5) > div > div:nth-child(2) > div > div.victimphoto > img"));
                user.setUserage(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_age"));
                // user.setUsergender(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_gender"));
                user.setUsergender(getGender(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_gender")));
                user.setUserethnic(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_ethnic"));
                user.setUserjob(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_job"));
                user.setUserorigin(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_origin"));
                user.setUserlocation(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_location"));
                user.setUserdetentiontype(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_detentiontype"));
                user.setUserdetentiontime(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_detentiontime"));
                user.setUserreason(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_reason"));
                user.setUserhealth(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_health"));
                user.setUserlatname(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victimlatname"));
                user.setUsercmnname(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victimcmnname"));
                user.setUseridno(getElement(2,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victimidno > span"));

                // user.setUserlists(getElement(3,doc,"body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_lists"));
                user.setUserlists(getVideoImageList(doc, 1));
                user.setUservideo(getVideoImageList(doc, 2));

                user.setComments1(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(6) > span.testord"));
                user.setComments2(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(8) > span.trans"));
                user.setComments3(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(10) > span.trans"));
                user.setComments4(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(12) > span.trans"));
                user.setComments5(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(14) > span.trans"));
                user.setComments6(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(16) > span.trans"));
                user.setComments7(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(18) > span.trans"));
                user.setComments8(getComments(doc,"body > div:nth-child(5) > div > p:nth-child(20) > span.trans"));


                System.out.println("user = " + user);
                saveOne(user);

            } catch (Exception e) {
                System.out.println("file = " + file);
                e.printStackTrace();
            }
        });
    }

    private String getGender(String element) {
        if( element !=null ){
            if("m".equalsIgnoreCase(element)){
                return "Mail";
            }
            if("f".equalsIgnoreCase(element)){
                return "Femail";
            }
        }
        return element;
    }

    private String getComments(Document doc, String selector) {

        Elements elements = null;
        try {
            elements = doc.select(selector);
            String htmlc = elements.html();
            return htmlc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";

    }


    int videostart = "https://www.youtube.com".length();
    int imagestart = ("https://"+pcConstants.address).length();
    private String getVideoImageList(Document doc, int type) {

        String ret = "";
        try {
            List<String> list = new ArrayList<String>();
            Elements elements = doc.select("body > div:nth-child(5) > div > div");
            for(Element e: elements){
                if(type==2){
                    for(Element ent: e.select("div:nth-child(2) > iframe")){
                        String src = ent.attr("src");
                        System.out.println("original video src = " + src);
                        if(src.indexOf("https://www.youtube.com")!=-1){
                            src = src.substring(videostart)+".mp4";
                        }
                        if(src.indexOf("facebook.com")!=-1){
                            continue;
                        }
                        if(src.indexOf("php")!=-1){
                            continue;
                        }
                        System.out.println("src. = " + src);
                        list.add(src);
                    }
                }
                if(type==1){
                    for(Element ent: e.select("div:nth-child(2) > a")){
                        String src = ent.attr("href");
                        System.out.println("original image src = " + src);
                        if(src.indexOf("https://"+pcConstants.address)!=-1){
                            src = src.substring(imagestart);
                        }
                        if(src.indexOf("php")!=-1){
                            continue;
                        }
                        System.out.println("src. = " + src);
                        list.add(src);
                    }
                    for(Element ent: e.select("div:nth-child(2) > span:nth-child(2) > a")){
                        String src = ent.attr("href");
                        System.out.println("original image src = " + src);
                        if(src.indexOf("https://"+pcConstants.address)!=-1){
                            src = src.substring(imagestart);
                        }
                        if(src.indexOf("php")!=-1){
                            continue;
                        }
                        System.out.println("src. = " + src);
                        list.add(src);
                    }

                }
            }
            ret = String.join(",",list);
            System.out.println("video size=" +list.size() +"  total="+ ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }


    String getElement(int type, Document doc,String selector) throws Exception {

        String retVal="";
        try {
            Elements elements = doc.select(selector);

            if( type==1 ){
                retVal =  elements.attr("src");
                if(retVal!=null && retVal.indexOf("https://"+pcConstants.address)!=-1){
                    retVal = retVal.substring(imagestart);
                }
            }

            if(type==2){
                String html = elements.html();
                if( html!=null){
                    String s1  =  html.replaceAll("<.*>.*</[\\w-\\W-]*>", "");
                    if( s1!=null ) retVal = s1.trim();
                }
            }

            if(type==3){

                // body > div:nth-child(5) > div > div:nth-child(2) > div > div.victim_lists > img
                List src = elements.select("img").eachAttr("src");
                Iterator it = src.iterator();
                while(it.hasNext()){

                    String nextval = (String)it.next();
                    if(nextval!=null && nextval.indexOf("https://"+pcConstants.address)!=-1){
                        nextval = nextval.substring(imagestart);
                    }
                    retVal+=nextval+",";

//                    retVal+=it.next()+",";
                }


            }

            // System.out.println("retVal = " + retVal);

        } catch (Exception e) {
            throw e;
        }
        return retVal;
    }
}
